package com.gem.web;

import com.gem.entity.Customer;
import com.gem.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @author zhang
 * @date 2020/10/4 18:03
 */
@Controller
@RequestMapping("/customer")
@SessionAttributes(value = {"customer"})
public class CustomerController {
    @Autowired
    CustomerService customerService;

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @ResponseBody
    @RequestMapping("/register")
    public String register(String username,String password){
        int i = customerService.register(username, password);
        if (i == 1){
            return "1";
        }
        //注册失败
        return "0";
    }


    /**
     * 登录方法
     * @param username
     * @param password
     * @param remeberMe
     * @param model
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public String login(String username, String password, String remeberMe, Model model, HttpServletRequest request, HttpServletResponse response){
        Customer customer = customerService.login(username,password);

        if (customer == null){
            //登录失败
            return "0";
        }

        if ("1".equals(remeberMe)){
            //使用记住我功能
            Cookie cookie = new Cookie("customerCookie",username + ":" + password);
            //设置最大保存时间
            cookie.setMaxAge(7 * 24 * 60 * 60);
            response.addCookie(cookie);

        }else {
            Cookie[] cookies = request.getCookies();
            Cookie customerCookie = getCookie(cookies, "customer");
            if (customerCookie != null){
                customerCookie.setMaxAge(0);
                response.addCookie(customerCookie);
            }
        }

        //利用session保存客户信息
        model.addAttribute("customer",customer);
        return "1";
    }

    /**
     * 退出方法
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session, SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "redirect:/index.jsp";
    }


    /**
     * 检查用户名
     * @param username
     * @return
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public String checkUsername(String username){
        Boolean bool = customerService.checkUsername(username);
        if (bool){
            return "1";
        }
        return "0";
    }

    /**
     * 跳转到更新信息页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdateCustomer")
    public String toUpdateCustomer(int id,Model model){
        Customer customer = customerService.findCustomerById(id);
        model.addAttribute("customer",customer);
        return "updateCustomer";
    }

    /**
     * 更新用户信息
     * @param id
     * @param name
     * @param phone
     * @param address
     * @param postCode
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateCustomer")
    public String updateCustomer(int id,String name,String phone,String address,int postCode){
        Customer customer = customerService.findCustomerById(id);
        customer.setName(name);
        customer.setAddress(address);
        customer.setPhone(phone);
        customer.setPostCode(postCode);

        customerService.eidtCustomer(customer);
        return "1";
    }





    private Cookie getCookie(Cookie[] cookies,String name){
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (c.getName().equals(name)){
                cookie = c;
                break;
            }
        }
        return cookie;
    }
}
